package com.cai310.lottery.entity.user;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.hibernate.type.EnumType;

import com.cai310.entity.CreateMarkable;
import com.cai310.entity.IdEntity;
import com.cai310.entity.UpdateMarkable;
import com.cai310.lottery.common.AgentDetailType;
import com.cai310.lottery.common.AgentLotteryType;
import com.cai310.lottery.common.DrawingOrderState;
import com.cai310.lottery.common.FundDetailType;
import com.cai310.lottery.common.Lottery;
import com.cai310.lottery.exception.DataException;

@Entity
@Table(name = com.cai310.lottery.Constant.LOTTERY_TABLE_PREFIX + "AGENT_PERSON_INFO")
public class AgentPersonInfo extends IdEntity implements CreateMarkable, UpdateMarkable, Serializable {
	private static final long serialVersionUID = 6420283302235215768L;
	// Fields
	// 充值，提现，投注，返点，撤单，追号
	private Long userId;
	private String userName;
	private String realName;
	private BigDecimal ips_money;
	private BigDecimal drawing_money;
	private BigDecimal prize_money;
	private BigDecimal bet_money;
	private BigDecimal luck_money;
	/** 用户的返点*/
	private Double rebate;
	private BigDecimal rebate_money;

	/**
	 * 彩票类型
	 * 
	 * @see com.cai310.lottery.common.AgentLotteryType
	 */
	private AgentLotteryType agentLotteryType;
	private Long timePos;///时间控制小时或者天。有相同的记录就吧统计合并
	/** 创建时间 */
	private Date createTime;

	/** 最后更新时间 */
	private Date lastModifyTime;
	
	/** 版本号,用于实现乐观锁 */
	private Integer version;

	// Constructors

	/** default constructor */
	public AgentPersonInfo() {
	}
	/**
	 * @throws DataException
	 */
	public void opr_money(BigDecimal money,AgentDetailType agentDetailType) throws DataException {
		if (money == null || money.doubleValue() <= 0)
			throw new DataException("增加的金额不能为空或小于等于零.");
		if(AgentDetailType.PAY.equals(agentDetailType)){
			this.ips_money = (this.ips_money == null) ? money : this.ips_money.add(money);
		}
		if(AgentDetailType.DRAWING.equals(agentDetailType)){
			this.drawing_money = (this.drawing_money == null) ? money : this.drawing_money.add(money);
		}
		if(AgentDetailType.PRIZE.equals(agentDetailType)){
			this.prize_money = (this.prize_money == null) ? money : this.prize_money.add(money);
		}
		if(AgentDetailType.BUY.equals(agentDetailType)){
			this.bet_money = (this.bet_money == null) ? money : this.bet_money.add(money);
		}
		if(AgentDetailType.REBATE.equals(agentDetailType)){
			this.rebate_money = (this.rebate_money == null) ? money : this.rebate_money.add(money);
		}
		if(AgentDetailType.LUCK.equals(agentDetailType)){
			this.luck_money = (this.luck_money == null) ? money : this.luck_money.add(money);
		}
	}
	/**
	 * @return the createTime
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false, updatable = false)
	public Date getCreateTime() {
		return createTime;
	}

	/**
	 * @param createTime the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	@Temporal(TemporalType.TIMESTAMP)
	@Column(insertable = false)
	public Date getLastModifyTime() {
		return lastModifyTime;
	}

	public void setLastModifyTime(Date lastModifyTime) {
		this.lastModifyTime = lastModifyTime;
	}
	/**
	 * @return {@link #version}
	 */
	@Version
	@Column(nullable = false)
	public Integer getVersion() {
		return version;
	}

	/**
	 * @param version the version to set
	 */
	public void setVersion(Integer version) {
		this.version = version;
	}
	@Column
	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}
	@Column
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	@Column
	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}
	@Column
	public BigDecimal getIps_money() {
		return ips_money;
	}

	public void setIps_money(BigDecimal ips_money) {
		this.ips_money = ips_money;
	}
	@Column
	public BigDecimal getDrawing_money() {
		return drawing_money;
	}

	public void setDrawing_money(BigDecimal drawing_money) {
		this.drawing_money = drawing_money;
	}
	@Column
	public BigDecimal getBet_money() {
		return bet_money;
	}

	public void setBet_money(BigDecimal bet_money) {
		this.bet_money = bet_money;
	}
	@Column
	public Long getTimePos() {
		return timePos;
	}

	public void setTimePos(Long timePos) {
		this.timePos = timePos;
	}
	@Column
	public BigDecimal getPrize_money() {
		return prize_money;
	}

	public void setPrize_money(BigDecimal prize_money) {
		this.prize_money = prize_money;
	}
	@Column
	public Double getRebate() {
		return rebate;
	}

	public void setRebate(Double rebate) {
		this.rebate = rebate;
	}
	@Column
	public BigDecimal getRebate_money() {
		return rebate_money;
	}

	public void setRebate_money(BigDecimal rebate_money) {
		this.rebate_money = rebate_money;
	}
	/**
	 * 彩票类型
	 * 
	 * @return {@link #lotteryType}
	 * @see com.cai310.lottery.common.Lottery
	 */
	@Type(type = "org.hibernate.type.EnumType", parameters = {
			@Parameter(name = EnumType.ENUM, value = "com.cai310.lottery.common.AgentLotteryType"),
			@Parameter(name = EnumType.TYPE, value = Lottery.SQL_TYPE) })
	@Column(nullable = true, updatable = false)
	public AgentLotteryType getAgentLotteryType() {
		return agentLotteryType;
	}

	/**
	 * 设置彩票类型
	 * 
	 * @param lotteryType {@link #lotteryType}
	 * @see com.cai310.lottery.common.Lottery
	 */
	public void setAgentLotteryType(AgentLotteryType agentLotteryType) {
		this.agentLotteryType = agentLotteryType;
	}
	@Column
	public BigDecimal getLuck_money() {
		return luck_money;
	}
	public void setLuck_money(BigDecimal luck_money) {
		this.luck_money = luck_money;
	}
	@Transient
	public BigDecimal getYingkui(){
		BigDecimal yingkui= BigDecimal.ZERO;
		if(null!=this.luck_money)yingkui=yingkui.add(luck_money);
		if(null!=this.prize_money)yingkui=yingkui.add(prize_money);
		if(null!=this.rebate_money)yingkui=yingkui.add(rebate_money);
		if(null!=this.bet_money)yingkui=yingkui.subtract(bet_money);
		return yingkui;
	}
}